package com.lifo.utils.quartz;

import com.lifo.good.bean.model.HfCategory;
import com.lifo.good.bean.model.HfGoods;
import com.lifo.good.service.SearchGoodCategoryService;
import com.lifo.good.service.SearchGoodsService;
import com.lifo.taobaoshop.bean.model.HfTaobaoShop;
import com.lifo.taobaoshop.service.HfTaobaoShopManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * Created by Administrator on 2018/5/10.
 */


@Configuration
@Component
@EnableScheduling
public class MatchGoodCategoryJob{

    @Autowired
    private SearchGoodsService searchGoodsService;

    @Autowired
    private SearchGoodCategoryService searchGoodCategoryService;

    @Autowired
    private HfTaobaoShopManagerService hfTaobaoShopManagerService;

    public void mathcGood(){

        final Integer PAGE_SIZE = 50;

        //查询未清洗的商品
        Integer noMatchGoodsCount = searchGoodsService.searchNoMatchGoodsCount();

        if(noMatchGoodsCount > 0){
            //查询商品类目
            List<HfCategory> categories = searchGoodCategoryService.searchHfCategory();

            //获取总页数
            Integer totalPage = noMatchGoodsCount % PAGE_SIZE == 0 ? noMatchGoodsCount % PAGE_SIZE : (noMatchGoodsCount / PAGE_SIZE) + 1;

            String [] categoryNames = null;

            for (int i = 0 ; i < totalPage ; i ++ ){
                Integer startIndex = i * PAGE_SIZE;
                List<HfGoods> goods = searchGoodsService.searchNoMatchGoodsByPage(startIndex, PAGE_SIZE);

                for (HfGoods good :goods){
                    A:for (HfCategory category : categories){
                        if(category.getChineseName().contains("&")){
                            //如果商品名字包含& 需要进行切割
                            categoryNames = category.getChineseName().split("&");
                            for (String cName : categoryNames){
                                if(good.getTitle().contains(cName)){
                                    good.setCategoryId(category.getSecondLevel());
                                    /////////增加批发价清洗 begin
                                    HfTaobaoShop hfTaobaoShop = hfTaobaoShopManagerService.getHfTaobaoShopByShopId(good.getShopId());
                                    if("-".equals(hfTaobaoShop.getDiscountType())){
                                            good.setWholesalePrice(good.getTaobaoPrice()-(hfTaobaoShop.getDiscount()*100));
                                    }else if("+".equals(hfTaobaoShop.getDiscountType())){
                                        good.setWholesalePrice(good.getTaobaoPrice()+(hfTaobaoShop.getDiscount()*100));
                                    }else if("/".equals(hfTaobaoShop.getDiscountType())){
                                        good.setWholesalePrice(good.getTaobaoPrice()/hfTaobaoShop.getDiscount());
                                    }
                                    /////////增加批发价清洗 end
                                    searchGoodsService.updateHfGoodsCategoryId(good);
                                    break A;
                                }
                            }
                        }else{
                            if(good.getTitle().contains(category.getChineseName())){
                                good.setCategoryId(category.getSecondLevel());
                                searchGoodsService.updateHfGoodsCategoryId(good);
                                break;
                            }
                        }
                    }
                }
            }
        }
    }
}
